<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Aliorder_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
        $this->config->load('userset');//用户积分
	}

    /**
     * @param $order_id
     * @return mixed
     * 查询单条
     */
    public function get_shop_orders_by_id($order_id){
        $query = $this->db
            ->select('*')
            ->get_where('shop_orders', array('order_id'=>$order_id));
        return $query->row_array();
    }

    /**
     * @param $order_no
     * @return mixed
     * 依据订单号查询单条
     */
    public function get_shop_orders_by_order_no($order_no){
        $query = $this->db
            ->select('*')
            ->get_where('shop_orders', array('order_no'=>$order_no));
        return $query->row_array();
    }
	
	/**
     * @param $order_no
     * @param $data
     * @return bool
     * 依据订单号修改订单信息
     */
    public function updage_shop_orders_by_order_no($order_no,$data){

        $this->db->where('order_no',$order_no);
        $this->db->update('shop_orders', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

	
    /**
     * @param $order_id
     * @param $data
     * @return bool
     * 修改订单信息
     */
    public function sava_shop_orders($order_id,$data){

        $this->db->where('order_id',$order_id);
        $this->db->update('shop_orders', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增订单
     */
    public function add_shop_orders($data){

        $this->db->insert('shop_orders',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    public function sucsess_order($order_id){

        $data = $this->get_shop_orders_by_id($order_id);

        $this->db->trans_start();
        // 更新订单状态为成功
        $this->db->set('order_status',2,false)->where('order_id',$order_id)->update('shop_orders');
        $this->db->set('good_update_time',time(),false)->where('order_id',$order_id)->update('shop_orders');
        // 商品库存减少
        $this->db->set('good_stock','good_stock-1',false)->where('good_id',$data['good_id'])->update('shop_goods');
        $this->db->set('good_orders','good_orders+1',false)->where('good_id',$data['good_id'])->update('shop_goods');
        // 用户积分增加+积分明细
        $this->db->set('user_credit','user_credit+'.$this->config->item('credit_jianlou'),false)->where('user_id',$data['user_id'])->update('users');
        $credit = array(
            "user_id"=>$data['user_id'],
            "credit_num"=>$this->config->item('credit_jianlou'),
            "credit_type"=>3,
            "credit_time"=>time(),
        );
        $this->db->insert('credits',$credit);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            return false;
        }else{
            return true;
        }
    }


    /**
     * 订单待确认
     * @param $data
     */
    public function wait_order($data){
		return $this->db->insert('shop_orders',$data);		
	}

    public function shop_order($data,$type=0){
        $this->db->trans_start();
        if($type == 0){
            $this->db->insert('shop_orders',$data);
            $order_id = $this->db->insert_id();
            $this->db->trans_complete();
            return $order_id;
        }
        if($type == 2){            
            // 成功逻辑
            // 更新订单状态为成功
            $this->db->set('order_status',$type,false)->where('order_id',$data['order_id'])->update('shop_orders');
            // 商品库存减少
            $this->db->set('good_stock','good_stock-1',false)->where('good_id',$data['good_id'])->update('shop_goods');
            // 订单数加一
            $this->db->set('good_orders','good_orders+1',false)->where('good_id',$data['good_id'])->update('shop_goods');
            // 账户明细
            $money = array(
                "user_id"=>$data['user_id'],
                "money_num"=>$data['order_price'],
                "money_item"=>$data['good_id'],
                "money_type"=>0, //0支付购买
                "money_time"=>time(),
            );
            $this->db->insert('moneys',$money);
            // 用户积分增加+积分明细
            $this->db->set('user_credit','user_credit+'.$this->config->item('credit_jianlou'),false)->where('user_id',$data['user_id'])->update('users');
            $credit = array(
                "user_id"=>$data['user_id'],
                "credit_num"=>$this->config->item('credit_jianlou'),
                "credit_type"=>3,
                "credit_time"=>time(),
            );
            $this->db->insert('credits',$credit);
            // 更新捡漏成功的时间
            $this->db->set('order_update_time',time(),false)->where('order_id',$data['order_id'])->update('shop_orders');
            //商家操作确认
            $this->db->set('order_shop_ok',1,false)->where('order_id',$data['order_id'])->update('shop_orders');
        }
        if($type == 1){
            // 失败，回滚
            $this->db->set('order_status',$type,false)->where('order_id',$data['order_id'])->update('shop_orders');
            $this->db->set('user_money','user_money+'.$data['order_price'],false)->where('user_id',$data['user_id'])->update('users');
            $money = array(
                "user_id"=>$data['user_id'],
                "money_num"=>$data['order_price'],
                "money_item"=>$data['good_id'],
                "money_type"=>1, //购买失败返回余额
                "money_time"=>time(),
            );
            $this->db->insert('moneys',$money);
            //商家操作确认
            $this->db->set('order_shop_ok',1,false)->where('order_id',$data['order_id'])->update('shop_orders');
        }     
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            return false;
        }else{
            return true;
        }  
        
    }


}
